﻿// $Id: CompositeActivityState.cs 10 2008-02-29 15:58:02Z mcartoixa $
/*
 *  Copyright (C) 2007 Noury Solutions
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 3 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 */
/*! \file
 * \brief Definition of \c Salamanca.DataActivities.CompositeActivityState.
 *
 * \author $Author: mcartoixa $
 * \date $Date: 2008-02-29 16:58:02 +0100 (ven., 29 févr. 2008) $
 * \version $Revision: 10 $
 */

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Text;
using Salamanca.DataRules;

namespace Salamanca.DataActivities.UI
{



    ///////////////////////////////////////////////////////////////////////////
    ///
    /// <summary>Interface of a composite activity state.</summary>
    ///
    ///////////////////////////////////////////////////////////////////////////

    internal interface ICompositeActivityState:
        IQuestionActivityState
    {
        IDictionary<string, IQuestionActivityState> InnerStates
        {
            get;
        }
    }



    ///////////////////////////////////////////////////////////////////////////
    ///
    /// <summary>Base implementation of a composite activity state.</summary>
    ///
    ///////////////////////////////////////////////////////////////////////////

    public abstract class CompositeActivityState<TActivityData>:
        QuestionActivityState<TActivityData>,
        ICompositeActivityState
        where TActivityData:
            IActivityData
    {

        /// <summary>Initializes a new instance of the <see cref="CompositeActivityState{TActivityData}" /> class.</summary>
        protected CompositeActivityState(IDictionary<string, IQuestionActivityState> innerStates, TActivityData data, IActivityParameters parameters):
            base(data, parameters)
        {
            _InnerStates=innerStates;
        }

        /// <summary>Creates the business rules used to <see cref="ActivityState{TActivityData}.Validate()"/> the activity state.</summary>
        /// <returns>The list of business rules created.</returns>
        protected override IList<IRule> CreateRules()
        {
            IList<IRule> ret=base.CreateRules();

            foreach (KeyValuePair<string, IQuestionActivityState> kvp in _InnerStates)
                ret.Add(
                    new ValidationDelegationRule<CompositeActivityState<TActivityData>>(
                        string.Format(
                            CultureInfo.CurrentCulture,
                            Resources.SR.InnerStateInvalidException,
                            kvp.Key
                        ),
                        kvp.Value,
                        string.Empty
                    )
                );

            return ret;
        }

        /// <summary>Creates the composite question associated to this comosite state.</summary>
        protected abstract CompositeQuestion CreateQuestions(IActivityController controller);

        /// <summary>Raises the <see cref="ActivityState{TActivityData}.Initialized" /> event.</summary>
        protected override void OnInitialized(ActivityStateEventArgs e)
        {
            ((IQuestionActivityState)this).Question=CreateQuestions(e.Controller);

            base.OnInitialized(e);
        }

        /// <summary>Gets the states that compose this activity state.</summary>
        public IDictionary<string, IQuestionActivityState> InnerStates
        {
            get
            {
                return _InnerStates;
            }
        }

        /// <summary>The states that compose this activity state.</summary>
        private IDictionary<string, IQuestionActivityState> _InnerStates;

    }

}
